package Hot100.Medium.BinaryTree;

import Hot100.Easy.LC104.TreeNode;

import java.util.*;

public class LC103_ZigZagLevelOrder {
    public static void main(String[] args) {

    }

    public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
        Queue<TreeNode> queue = new ArrayDeque<>();
        List<List<Integer>> res = new ArrayList<>();
        if(root != null){
            queue.add(root);
        }
        while(!queue.isEmpty()){
            LinkedList<Integer> level = new LinkedList<>();
            int leverSize = queue.size();
            for(int i = 0; i < leverSize; i++){
                TreeNode cur = queue.poll();
                if(res.size() %2 == 0){
                    level.addLast(cur.val);
                } else {
                    level.addFirst(cur.val);
                }
                if (cur.left != null) queue.add(cur.left);
                if (cur.right != null) queue.add(cur.right);
            }
            res.add(level);
        }
        return res;
    }
}
